package com.qujia.station.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.station.domain.CouponPushMessageRule;
import com.qujia.station.domain.dto.CouponPushMessageRuleDto;
import com.qujia.station.enums.CouponPushRuleEnum;
import com.qujia.station.mapper.AppUserInfoMapper;
import com.qujia.station.mapper.CouponPushMessageRuleMapper;
import com.qujia.station.service.CouponPushMessageRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CouponPushMessageRuleServiceImpl extends ServiceImpl<CouponPushMessageRuleMapper, CouponPushMessageRule> implements CouponPushMessageRuleService {


    @Autowired
    private AppUserInfoMapper appUserInfoMapper;

    @Override
    public Boolean addOrUpdateCouponPushMessageRule(CouponPushMessageRuleDto couponPushMessageRuleDto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(null==user){
            throw new RuntimeException("当前用户不存在");
        }
        try{
            LambdaQueryWrapper<CouponPushMessageRule> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CouponPushMessageRule::getEnterpriseId,user.getEnterpriseId());
            List<CouponPushMessageRule> list = list(wrapper);
            if(CollectionUtil.isNotEmpty(list)){
                lambdaUpdate().eq(CouponPushMessageRule::getEnterpriseId,user.getEnterpriseId())
                        .set(CouponPushMessageRule::getCouponSwitch,couponPushMessageRuleDto.getCouponSwitch())
                        .set(CouponPushMessageRule::getPushTime,couponPushMessageRuleDto.getPushTime())
                        .set(CouponPushMessageRule::getRule,couponPushMessageRuleDto.getRule())
                        .set(CouponPushMessageRule::getBeforeRule,list.get(0).getRule())
                        .update();
                //跟新推送状态
                appUserInfoMapper.updatePushStatusByEnterpriseId(list.get(0).getEnterpriseId());
            }else {
                CouponPushMessageRule couponPushMessageRule = BeanUtil.copyProperties(couponPushMessageRuleDto, CouponPushMessageRule.class);
                couponPushMessageRule.setBeforeRule(couponPushMessageRuleDto.getRule());
                couponPushMessageRule.setEnterpriseId(user.getEnterpriseId());
                couponPushMessageRule.setStationId(user.getStationId());
                couponPushMessageRule.setCreateUser(user.getUserName());
                couponPushMessageRule.setCreateTime(new Date());
                save(couponPushMessageRule);
            }
            return Boolean.TRUE;
        }catch (RuntimeException e){
            log.error("新增或修改优惠券消息推送规则失败：{}",e.getMessage());
            return Boolean.FALSE;
        }
    }

    @Override
    public CouponPushMessageRule selectCouponPushMessageRule() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(null==user){
            throw new RuntimeException("当前用户不存在");
        }
        LambdaQueryWrapper<CouponPushMessageRule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CouponPushMessageRule::getEnterpriseId,user.getEnterpriseId());
        CouponPushMessageRule couponPushMessageRule = getOne(wrapper);
        return couponPushMessageRule;
    }
}
